﻿$(document).ready(function () {
    $("div.heading").click(function () {
        var extraHieght = 0;
        if ($('#message_div').length) {
            extraHieght = $('#message_div').height() + 4;
        }
        var e = document.getElementById('application_top_panel');
        if (e != null) {
            if (e.style.display == 'none') {
                e.style.display = 'block';
                var topPalenHeight = $("#application_top_panel").height();
                myLayout.open('north');
                myLayout.open('west');
                $('.headingText').removeClass('headingTextActive');
                $('.bDiv').css('height', (getFullGridHeight() - topPalenHeight - 2) - extraHieght);  // used 2 for #application_top_panel padding
            }
            else {
                e.style.display = 'none';
                $('.headingText').addClass('headingTextActive');
                $('.bDiv').css('height', getFullGridHeight() + 2 - extraHieght);
            }
        }
    });

    // for left menu click function

    $('.dockMenuText').click(function () {
        clearDocMenuHover();
        $(this).addClass('dockMenuTextActive');
    });

    $('#dockMenuBudget').click(function () {
        renderBudgetMenu(null);
    });
    $('#dockMenuProc').click(function () {
        renderProcurementMenu(null);
    });
    $('#dockMenuInv').click(function () {
        renderInventoryMenu(null);
    });
    $('#dockMenuAcc').click(function () {
        renderAccountingMenu(null);
    });
    $('#dockMenuSettings').click(function () {
        renderDefaultMenu(null);
    });

});


//***********************



//***********************


function trimFormValues(form) {

    // iterate over all of the inputs for the form
    $(':input', form).each(function () {
        var type = this.type.toLowerCase();
        var tag = this.tagName.toLowerCase(); // normalize case

        // for inputs and textareas
        if (type == 'text' || type == 'hidden' || tag == 'textarea')
            this.value = jQuery.trim(this.value);
    });
}

function clearForm(frm) {
    clearErrors(frm);
    clearFormValues(frm);
}

function clearFormValues(form) {

    // iterate over all of the inputs for the form

    $(':input', form).each(function () {
        var type = this.type;
        var tag = this.tagName.toLowerCase(); // normalize case

        // password inputs, and textareas
        if (type == 'text' || type == 'password' || type == 'hidden' || tag == 'textarea') {
            this.value = "";
        }

        // checkboxes and radios need to have their checked state cleared
        else if (type == 'checkbox' || type == 'radio')
            this.checked = false;

        // select elements need to have their 'selectedIndex' property set to -1
        else if (tag == 'select') {
            this.selectedIndex = -1;
            this.value = '-1';
        }
    });

}
function clearErrors(frm) {
    //        var validator = frm.validate();
    var validator = $(frm).validate();

    try {
        validator.resetForm();
    } catch (e) {
    }


    $('input.error', frm).each(function () {
        $(this).removeClass('error');
    });

    $('label.error', frm).each(function () {
        $(this).remove();
    });
}
function setButtonDisabled(button, isEnable) {
    $(button).attr('disabled', isEnable);
}
 
function onLoadMainLayout() {
    myLayout = $('body').layout({
        west__size: 300
                        , west__onresize: function () {
                            $("#left-accordion").accordion("resize");
                        }
        , west__closable: false
        , west__resizable: true
		, west__maxSize: "17%"
		, west__minSize: "10%"
        , west__togglerContent_open: "&#8249;" // "‹"
        , west__togglerContent_closed: "&#8250;" // "›" 
        , center__contentSelector: '#contentHolder'
        , north: {
            size: "auto"
			, spacing_open: 0
			, closable: false
			, resizable: false
        }

    });

    // ACCORDION - in the West pane
    $("#left-accordion").accordion({
        fillSpace: true
                , active: 0
    });
}


function bindAutoLoadClass() {
    $('div.ui-layout-west a.autoload').click(function (e) {
        var href = $(this).attr('href');
        var currentUrl = document.location.href;
        var currentPage = currentUrl.substring(currentUrl.indexOf('#', 0), currentUrl.length);
        if (currentPage == href) {
            var reqUrl = href.substring(1, href.length);
            load(reqUrl);
        }
    });
}

function afterAjaxError(XMLHttpRequest, textStatus) {

    if (XMLHttpRequest == undefined) {
        return false;
    }
    if (XMLHttpRequest.status == 0) {
        alert('You are offline now !!\n Please Check Your Network Connection.');
    } else if (XMLHttpRequest.status == 404) {
        alert('Requested URL not found.');
        showLoadingSpinner(false);
    } else if (XMLHttpRequest.status == 500) {
        alert('Internel Server Error.');
    } else if (textStatus == 'parsererror') {
        alert('Error.\nParsing JSON Request failed.');
    } else if (textStatus == 'timeout') {
        alert('Request Time out.');
    } else {
        alert('Unknow Error.\n' + XMLHttpRequest.responseText);
    }
    return false;
}

function showLoadingSpinner(show) {
    if (show) {
        $('#spinner').show();
    } else {
        $('#spinner').hide();
    }
}
